/**
 *
 * Copyright 2010 TeleNav, Inc. All rights reserved.
 * EntryModel.java
 *
 */
package com.telenav.module.activitylist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.google.protobuf.InvalidProtocolBufferException;
import com.telenav.framework.mvc.AbstractModel;
import com.telenav.framework.network.HostProvider;
import com.telenav.framework.network.INetworkCallback;
import com.telenav.framework.network.INetworkProxyConstants;
import com.telenav.framework.network.NetworkProxy;
import com.telenav.j2me.datatypes.ProtocolBuffer;
import com.telenav.protobuf.ActivityDetailProtoResp;
import com.telenav.protobuf.ActivityListProtoResp;

class ActivityListModel extends AbstractModel implements IActivityListConstants, INetworkCallback
{  
	private List<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();
	private Hashtable<String, String> activityObj = new Hashtable<String, String>();
	private String[] images;
	@Override
	protected void doAction(int actionId) {
        switch (actionId)
        {
            case ACTION_INIT:
            {
            	put(KEY_O_ACTIVITY_DATA, mData);
    			if(getInt(KEY_I_START_INDEX) > 0)
    			{
    				put(KEY_B_IS_LOADED, true);
    			}
    			else
    			{
    				put(KEY_I_START_INDEX, 1);
    				put(KEY_I_END_INDEX, ITEM_SIZE_PER_PAGE);
    			}
            	
            	if(getBool(KEY_B_IS_LOADED) && getInt(KEY_I_START_INDEX) < ITEM_SIZE_PER_PAGE)
            	{
            		postModelEvent(EVENT_MODEL_UPDATE_ACTIVITYLIST);
            	}
            	else
            	{
            		requestActivities();
            	}
                break;
            }
            case ACTION_LOAD_DETAIL:
            {
            	requestDetail();
            	break;
            }
        }
	}
	
	private void requestDetail()
	{
		new Thread(new Runnable()
		{
			public void run()
			{
		    	Map<String, Object> params = new HashMap<String, Object>();
		    	params.put("id", "" + ActivityListModel.this.getInt(KEY_I_SELECT_ID));
		    	NetworkProxy proxy = new NetworkProxy(ActivityListModel.this);
		    	proxy.sendRequest(params, INetworkProxyConstants.ACT_ACTIVITY_DETAIL);
			}
		}).start();
	}
	
	private void requestActivities()
	{
		new Thread(new Runnable()
		{
			public void run()
			{
		    	Map<String, Object> params = new HashMap<String, Object>();
		    	params.put("startindex", ActivityListModel.this.getInt(KEY_I_START_INDEX));
		    	params.put("endindex", ActivityListModel.this.getInt(KEY_I_END_INDEX));
		    	NetworkProxy proxy = new NetworkProxy(ActivityListModel.this);
		    	proxy.sendRequest(params, INetworkProxyConstants.ACT_ACTIVITY_LIST);
			}
		}).start();
	}
	
	@Override
	public void handleProtocolBuffer(ProtocolBuffer buf) 
	{
		if(buf.getObjType().equals(INetworkProxyConstants.ACT_ACTIVITY_LIST))
		{
			try {
				ActivityListProtoResp.ActivityList resp = ActivityListProtoResp.ActivityList.parseFrom(buf.getBufferData());
				parseActivityData(resp);
			} 
			catch (InvalidProtocolBufferException e) 
			{
				e.printStackTrace();
			}
		}
		else if(buf.getObjType().equals(INetworkProxyConstants.ACT_ACTIVITY_DETAIL))
		{
			ActivityDetailProtoResp.ActivityDetail resp;
			try {
				resp = ActivityDetailProtoResp.ActivityDetail.parseFrom(buf.getBufferData());
				this.parseActivityDetailData(resp);
			} catch (InvalidProtocolBufferException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void parseActivityDetailData(ActivityDetailProtoResp.ActivityDetail resp)
	{
		activityObj.put("overview", resp.getOverview());
		activityObj.put("organizer", resp.getOrganizer());
		activityObj.put("detail", resp.getDetail());
		images = new String[resp.getImagearrayCount()];
		resp.getImagearrayList().toArray(images);
		
		put(KEY_O_ACTIVITYDETAIL, activityObj);
		put(KEY_O_IMAGEARRAY, images);
		
		postModelEvent(EVENT_MODEL_UPDATE_ACTIVITYDETAIL);
	}
	
	private void parseActivityData(ActivityListProtoResp.ActivityList resp)
	{
		if(getBool(KEY_B_IS_FINISH) && (getInt(KEY_I_DATA_SIZE) == resp.getDatasize()))
		{
			postModelEvent(EVENT_MODEL_UPDATE_ACTIVITYLIST);
			return;
		}
		else
		{
			put(KEY_I_DATA_SIZE, resp.getDatasize());
			put(KEY_B_IS_FINISH, false);
		}
		for(int i = 0; i < resp.getActivityCount(); i++)
		{
			Map<String, Object> map = new HashMap<String, Object>();
	        map.put("title", resp.getActivity(i).getTitle());
	        map.put("time", resp.getActivity(i).getTime());
	        map.put("place1", resp.getActivity(i).getPlace1());
	        map.put("place2", resp.getActivity(i).getPlace2());
	        map.put("count", resp.getActivity(i).getCount());
	        map.put("residual_cnt", resp.getActivity(i).getResidualCount());
	        map.put("img", HostProvider.getImageUrl() + "/"+ resp.getActivity(i).getImg());
	        mData.add(map);
		}
		
		postModelEvent(EVENT_MODEL_UPDATE_ACTIVITYLIST);
	}
}
